// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#include "coin2/exchange/bybit/api_order_v3/native_rest_async_asio_subscriber.h"

namespace coin2::exchange::bybit::api_order_v3 {

void BybitRestAsyncAsioSubscriber::QueryAccountBalance() {
  RestParams params;
  auto http_context = rest_client()->QueryAccountBalance();
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitRestAsyncAsioSubscriber::QueryOpenOrders() {
  RestParams params;
  auto http_context = rest_client()->QueryOpenOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitRestAsyncAsioSubscriber::QueryHistoryOrders() {
  RestParams params;
  auto http_context = rest_client()->QueryHistoryOrders(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitRestAsyncAsioSubscriber::QueryFills() {
  RestParams params;
  auto http_context = rest_client()->QueryFills(params);
  if (!http_context) {
    return;
  }
  http_context->handler =
      std::bind(&BybitRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void BybitRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&BybitRestAsyncAsioSubscriber::QueryAccountBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  AddPeriodicTask(
      std::bind(&BybitRestAsyncAsioSubscriber::QueryOpenOrders, this),
      config_.query_open_orders_period_sec(),
      "query_open_orders");
  AddPeriodicTask(
      std::bind(&BybitRestAsyncAsioSubscriber::QueryHistoryOrders, this),
      config_.query_cancel_orders_period_sec(),
      "query_history_orders");
  AddPeriodicTask(
      std::bind(&BybitRestAsyncAsioSubscriber::QueryFills, this),
      config_.query_fill_period_sec(),
      "query_fills");
}

}  // namespace coin2::exchange::bybit::api_order_v3
